package com.tyl.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.log.Log;
import com.tyl.ResultEntity;
import com.tyl.entity.Category;
import com.tyl.service.ICategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author 汤云龙
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private ICategoryService categoryService;
    @GetMapping("/selectChildrenCategory/{categoryId}")
    public ResultEntity selectChildrenCategory(@PathVariable("categoryId") Long cid){
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.eq("parent_id",cid);
        List<Category> categoryList = categoryService.list();
        return ResultEntity.ok(categoryList);
    }
    @PostMapping("/add")
    public ResultEntity addCategory(@RequestBody Category category){
        categoryService.save(category);
        return ResultEntity.ok("添加成功");
    }
    @DeleteMapping("/delete/{categoryId}")
    public ResultEntity deleteById(@PathVariable("categoryId") Integer categoryId){
        //根据Id删除类别
        categoryService.removeById(categoryId);
        return ResultEntity.ok("删除成功");
    }
    @GetMapping("/selectOne/{categoryId}")
    public ResultEntity selectCategory(@PathVariable ("categoryId") Integer categoryId){
        //查询类别
        Category category = categoryService.getById(categoryId);
        return ResultEntity.ok(category);
    }
    @PutMapping("/update")
    public ResultEntity updateCategory(@RequestBody Category category){
        //修改类别
        QueryWrapper<Category> categoryQueryWrapper = new QueryWrapper<>();
         categoryQueryWrapper.eq("category_id", category.getCategoryId());
        categoryService.update(category, categoryQueryWrapper);
        return ResultEntity.ok("修改成功");
    }

}

